﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using global::TerhelesEloszto.Common;

using global::TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class Fact : System.Web.UI.Page
	{
		protected global::TerhelesEloszto.Engine.IEngine engine = global::TerhelesEloszto.Engine.Engine.Instance();
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			if( Request.QueryString["msg"] != null )
			{
				if( Request.QueryString["msg"].Equals( "1" ) ) MessageLabel.Text = "A műveletet sikeresen végrehajtottuk!";
				else if( Request.QueryString["msg"].Equals( "0" ) ) MessageLabel.Text = "Hiba történt a művelet végrehajtása közben";
			}
			try
			{
				loadTable();
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/Fact.aspx?msg=0" );
			}
		}

		public void loadTable()
		{


			TableCell tCellHeader1 = new TableCell();
			tCellHeader1.Text = "Neptun";
			TableRow tRow = new TableRow();
			tRow.Cells.Add( tCellHeader1 );
			TableFact.Rows.Add( tRow );

			TableCell tCellHeader2 = new TableCell();
			tCellHeader2.Text = "Név";

			tRow.Cells.Add( tCellHeader2 );
			TableFact.Rows.Add( tRow );

			TableCell tCellHeader3 = new TableCell();
			tCellHeader3.Text = "Leírás";
			tRow.Cells.Add( tCellHeader3 );

			TableCell tCellHeader4 = new TableCell();
			tCellHeader4.Text = "";
			tRow.Cells.Add( tCellHeader4 );

			TableCell tCellHeader5 = new TableCell();
			tCellHeader5.Text = "";
			tRow.Cells.Add( tCellHeader5 );

			tRow.CssClass = "bold";
			TableFact.Rows.Add( tRow );

			TableFact.GridLines = GridLines.Both;
			TableFact.BorderStyle = BorderStyle.Groove;
			TableFact.CellPadding = 15;


			int i = 0;
			foreach( IFact actFact in engine.GetFacts() )
			{
				TableRow actRow = new TableRow();

				TableCell actc1 = new TableCell();
				actc1.Text = actFact.Neptun;
				actRow.Cells.Add( actc1 );

				TableCell actc2 = new TableCell();
				actc2.Text = actFact.Name;
				actRow.Cells.Add( actc2 );


				TableCell actc3 = new TableCell();
				actc3.Text = actFact.Description;
				actRow.Cells.Add( actc3 );

				TableCell actc4 = new TableCell();
				actc4.Text = "<a href=\"YesNo.aspx?ID=" + actFact.Neptun + "&T=factdelete&Q=Biztosan törli a tárgyat\">Eltávolítás</a>";
				actRow.Cells.Add( actc4 );

				TableCell actc5 = new TableCell();
				actc5.Text = "<a href=\"ModifyFact.aspx?ID=" + actFact.Neptun.Trim() + "\">Módosítás</a>";
				//actc5.Text = "aaa";
				actRow.Cells.Add( actc5 );

				//TableFact.Font.Name = "Arial";
				//TableFact.Font.Bold = true;
				TableFact.Rows.Add( actRow );
			}
		}

		protected void Button1_Click( object sender, EventArgs e )
		{
			Response.Redirect( "~/Admin/CreateFact.aspx" );
		}
	}
}